// pages/lost.js
const app = getApp()

Page({
  data: {
    imgUrls: [
      'http://47.103.218.55/20210219185902.jpg',
      'http://47.103.218.55/20210219185856.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    region: ['请选择地址  >', '', ''],
    isOnSchool: false,
    school: '',
    postType: 0,
    itemType: 0,
    itemType2: 0,
    findList: [],
    topScroll: 0,
    senderUid: wx.getStorageSync('userinfo').uid
  },
  async onLoad() {
    app.editTabbar();
    app.receiverUnRead();
    if (wx.getStorageSync('region') != '') {
      this.setData({
        region: wx.getStorageSync('region'),
        school: wx.getStorageSync('school')
      })
    }
    this.setData({
      senderUid: wx.getStorageSync('userinfo').uid
    })
    await wx.User.findPosts({
      data: {
        province: this.data.region[1] == '' ? '' : this.data.region[0],
        city: this.data.region[1],
        address: this.data.region[2],
        school: this.data.school,
        postType: this.data.postType,
        itemType: this.data.itemType
      }
    })
    this.setData({
      findList: wx.getStorageSync('findList')
    })
    wx.stopPullDownRefresh()
  },
  subSwiper(e) {
    if (e.detail.scrollTop < 230) {
      this.setData({
        topScroll: e.detail.scrollTop
      })
    }

  },
  sendMessage(e) {
    if (e.currentTarget.dataset.uid != this.data.senderUid) {
      wx.navigateTo({
        url: '../message/sendMessage?uid=' + e.currentTarget.dataset.uid + '&alias=' + e.currentTarget.dataset.alias + '&avatar=' + e.currentTarget.dataset.avatar,
      })
    }

  },
  async onShow() {
    await this.onLoad()
    
  },
  //刷新
  onRefresh() {
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.onLoad();
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  },
  keywordIn(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  async search() {
    if (this.data.region[1] == '') {
      wx.navigateTo({
        url: './search?keyword=' + this.data.keyword + '&province=' + '' + '&city=' + '' + '&address=' + '' + '&school=' + this.data.school,
      })
    } else {
      wx.navigateTo({
        url: './search?keyword=' + this.data.keyword + '&province=' + this.data.region[0] + '&city=' + this.data.region[1] + '&address=' + this.data.region[2] + '&school=' + this.data.school,
      })
    }

  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
    wx.setStorageSync('region', this.data.region)
    this.onLoad()
  },
  schoolIn(e) {
    this.setData({
      school: e.detail.value,
    })
    wx.setStorageSync('school', this.data.school)
    this.onLoad()
  },
  async findTab(e) {
    this.setData({
      postType: e.detail.currentIndex
    })
    await wx.User.findPosts({
      data: {
        province: this.data.region[1] == '' ? '' : this.data.region[0],
        city: this.data.region[1],
        address: this.data.region[2],
        school: this.data.school,
        postType: e.detail.currentIndex,
        itemType: e.detail.currentIndex == 0 ? this.data.itemType : this.data.itemType2
      }
    })
    this.setData({
      findList: wx.getStorageSync('findList')
    })
  },
  async findSub(e) {
    if (this.data.postType == 0) {
      this.setData({
        itemType: e.detail.currentIndex
      })
    } else {
      this.setData({
        itemType2: e.detail.currentIndex
      })
    }
    await wx.User.findPosts({
      data: {
        province: this.data.region[1] == '' ? '' : this.data.region[0],
        city: this.data.region[1],
        address: this.data.region[2],
        school: this.data.school,
        postType: this.data.postType,
        itemType: e.detail.currentIndex
      }
    })
    this.setData({
      findList: wx.getStorageSync('findList')
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '已经到底啦！',
      icon: 'none'
    })
  },

})